package com.tfswue.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.group.entity.Product;
import com.tfswue.group.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/products")
@Tag(name = "商品接口", description = "符合RESTful风格的商品管理接口")
public class ProductController {
    @Autowired
    private ProductService productService;

    public ProductController() {
        log.info("ProductController 已初始化");
    }

    @Operation(summary = "查询所有商品", description = "获取系统中所有商品的列表")
    @ApiResponse(responseCode = "200", description = "成功获取商品列表")
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.list();
    }

    @Operation(summary = "查询单个商品", description = "根据ID查询商品信息")
    @ApiResponse(responseCode = "200", description = "成功获取商品信息")
    @Cacheable(value = "dataCache", key = "#id")
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Integer id) {
        log.info("查询商品ID: {}", id);
        return productService.getById(id);
    }

    @Operation(summary = "添加商品", description = "发布新商品")
    @ApiResponse(responseCode = "200", description = "成功添加商品")
    @PostMapping
    public boolean addProduct(@RequestBody Product product) {
        return productService.save(product);
    }

    @Operation(summary = "更新商品", description = "更新商品信息")
    @ApiResponse(responseCode = "200", description = "成功更新商品")
    @CachePut(value = "dataCache", key = "#product.id")
    @PutMapping
    public boolean updateProduct(@RequestBody Product product) {
        return productService.updateById(product);
    }

    @Operation(summary = "删除商品", description = "根据ID删除商品")
    @ApiResponse(responseCode = "200", description = "成功删除商品")
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable Integer id) {
        return productService.removeById(id);
    }

    @Operation(summary = "分页查询商品", description = "分页获取商品列表")
    @ApiResponse(responseCode = "200", description = "成功分页获取商品")
    @GetMapping("/querypage/{page}/{size}")
    public List<Product> getProductsByPage(@PathVariable Integer page, @PathVariable Integer size) {
        Page<Product> productPage = new Page<>(page, size);
        return productService.page(productPage).getRecords();
    }

    @Operation(summary = "条件分页查询商品", description = "根据条件分页查询商品")
    @ApiResponse(responseCode = "200", description = "成功条件查询商品")
    @GetMapping("/querylist/{page}/{size}")
    public List<Product> queryPageByCondition(@PathVariable("page") Integer page,
                                              @PathVariable("size") Integer size,
                                              @RequestBody(required = false) Product product) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (product != null) {
            if (product.getName() != null) {
                queryWrapper.like("name", product.getName());
            }
            if (product.getCategoryId() != null) {
                queryWrapper.eq("category_id", product.getCategoryId());
            }
        }
        Page<Product> productPage = new Page<>(page, size);
        return productService.page(productPage, queryWrapper).getRecords();
    }
}